package com.ljw.service;

import com.github.wxpay.sdk.WXPayConstants;
import com.ljw.common.HttpClient;
import com.ljw.common.UUIDUtils;
import com.github.wxpay.sdk.WXPayUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijunwei
 * @Time: 2020/3/30 12:59
 * appid=wx8397f8696b538317   # 微信公众账号或开放平台APP的唯一标识
 * partner=1473426802         #   mch_id：商户号  (配置文件中的partner)
 * partnerkey=8A627A4578ACE384017C997F12D68B23  #   商户密钥
 * notifyurl=http://a31ef7db.ngrok.io/WeChatPay/WeChatPayNotify  # 回调地址
 */
public class WeixinPayServiceImpl implements WeixinPayService {

    private String appid = "wx8397f8696b538317";
    private String mch_id = "1473426802";
    private String key = "8A627A4578ACE384017C997F12D68B23";

    /**
     * 生成二维码
     *
     * @return
     */
    public Map createNative(String out_trade_no, String total_fee) throws Exception {
        //1.创建参数
        Map<String, String> param = new HashMap();//创建参数
        param.put("appid", "wx8397f8696b538317");//公众号
        param.put("mch_id", "1473426802");//商户号
        param.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
        param.put("body", "商品描述");//商品描述
        param.put("out_trade_no", out_trade_no);//商户订单号
        param.put("total_fee", total_fee);//总金额（分）
        param.put("spbill_create_ip", "127.0.0.1");//IP
        param.put("notify_url", "http://a31ef7db.ngrok.io/WeChatPay/WeChatPayNotify");//回调地址(随便写)
        param.put("trade_type", "NATIVE");//交易类型

        try {
            //2.生成要发送的xml
            String xmlParam = WXPayUtil.generateSignedXml(param, "8A627A4578ACE384017C997F12D68B23");
            System.out.println("生成要发送的xml:" + xmlParam);
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setHttps(true);
            client.setXmlParam(xmlParam);
            client.post();
            //3.获得结果
            String result = client.getContent();
            System.out.println("第三步:" + result);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
            System.out.println("---------------" + resultMap.toString());
            Map<String, String> map = new HashMap<String, String>();
            map.put("code_url", resultMap.get("code_url"));//支付地址  关键
            map.put("total_fee", total_fee);//总金额
            map.put("out_trade_no", out_trade_no);//订单号
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<String, String>();
        }
    }

    @Test
    public void test01() throws Exception {
        WeixinPayServiceImpl weixinPayServiceImpl = new WeixinPayServiceImpl();
        String uuid = UUIDUtils.getUUID();
        Map map = weixinPayServiceImpl.createNative(uuid, "1");
        System.out.println("-----" + map.toString());
    }

    @Override
    public Map queryPayStatus(String out_trade_no) {
        Map param = new HashMap();
        param.put("appid", "wx8397f8696b538317");//公众账号ID
        param.put("mch_id", "1473426802");//商户号
        param.put("out_trade_no", out_trade_no);//订单号
        param.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";
        try {
            String xmlParam = WXPayUtil.generateSignedXml(param, "8A627A4578ACE384017C997F12D68B23");
            HttpClient client = new HttpClient(url);
            client.setHttps(true);
            client.setXmlParam(xmlParam);
            client.post();
            String result = client.getContent();
            Map<String, String> map = WXPayUtil.xmlToMap(result);
            System.out.println("-----------------:"+map.toString());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    

}

